<template>
  <div class='reserbox'>
    <div style="margin-bottom:40px;padding-top:10px;text-align:right"> <i class="el-icon-error" style="background:#e6e6e6" @click="resetForm"></i></div>
    <div class=''>
      <div class="box">
        <div style="line-height:240px"> 营业执照正面：</div>
        
          <el-upload
            class="avatar-uploader"
            action="https://upload.qiniup.com/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :data="postData">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon">
               <span class="imgBox">点击添加图片</span>
            </i>
          </el-upload>
      </div>
      <div class="box">
        <div> <div style="line-height:240px">医疗许可正面：</div></div>
        
        <el-upload
          class="avatar-uploader"
          action="https://upload.qiniup.com/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess1"
          :before-upload="beforeAvatarUpload"
          :data="postData">
          <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon">
            <span class="imgBox">点击添加图片</span>
          </i>
          
        </el-upload>
      </div>
      <div v-if="result_msg" style="color:red;text-align:center;margin: -20px 0 20px -50px">反馈信息：{{result_msg}}</div>
      <div class="box">
         <div style="color:#ccc">信息提交后，牙记将1-2个工作日内完成审核</div>
        <el-button type="warning" @click="submitForm(1)" style="margin-left:-40px;margin-right:40px">保 存</el-button>
        <el-button type="primary" @click="submitForm(0)">提交审核</el-button>
       
      </div>
    </div>
  </div>
</template>

<script>
import Http from '@/Http/Http'

export default {
  
  data () {
    const title = (rule,value,callback) => {
      if (value === '') {
        callback(new Error('请输入标题'))
      } else {
        callback()
      }
    }
    
    return {
     
      postData:{
        token:'',
      },
      tokenimgUrl:'',
      imageUrl: '',
      imageUrl1: '',
      result_msg:'',
      ruleForm2:{

      }
      
    }
    
  },

  mounted () {
    
  },
  created() {
    this.getToken()
  },
  methods:{
    getToken() {
      Http.getCertification()
      .then( res => {
        this.imageUrl = res.data.data.front;
        this.imageUrl1 = res.data.data.behind;
        this.result_msg = res.data.data.result_msg
      })
      .catch( error => {
        //this.$router.push('/')
        console.log(error)
      })
      Http.QiniuToken({})
      .then( res => {
        this.postData.token = res.data.data.uptoken;
        this.tokenimgUrl = res.data.data.domain
      })
      .catch( error => {
        this.$router.push('/')
        console.log(error)
      })
    },
    resetForm() {
      this.$router.push('Push')
    },
    submitForm(val){
      Http.addCertification({
        "front":this.imageUrl,
        "behind":this.imageUrl1,
        "is_draft":val,
      })
      .then(res => {
        if (res.data.code == 200) {
          if(val==1){
            this.$message.success('保存成功')
          }else if (val ==0 ){
            this.$message.success('提交成功，请耐心等候审核状态')
          }
        } else {
          this.$message.error('提交失败')
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = `http://${this.tokenimgUrl}/${res.key}`;
    },
    handleAvatarSuccess1(res, file) {
      this.imageUrl1 = `http://${this.tokenimgUrl}/${res.key}`;
      console.log(this.imageUrl1)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      if (isJPG || isPNG) {
        return true;
      } else {
          this.$message.error('上传图片格式不正确!');
        return false;
      }
    }
  },
}
</script>
<style scoped>
.reserbox{
  background: #fff;
  width: 60%;
  margin-left:10%;
  padding:0px 20px 40px 0;
}
.box{
  display:flex;
  justify-content:center;
  margin-bottom:40px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 388px;
    height: 240px;
    line-height: 240px;
    text-align: center;
    position: relative;
    top:-20px;
  }
  .avatar {
    width: 388px;
    height: 240px;
    display: block;
  }
  .imgBox{
    position: absolute;
    left: 100px;
    top: 50px;
    height: 40px;
    width: 200px;
  }
</style>
